package com.cnooc.aicv.lmapp.service;


import com.cnooc.aicv.lmapp.model.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
public class JsonParserService {
    @Autowired
    LmDiagFilterImp filterImp;
    //private final ObjectMapper objectMapper = new ObjectMapper();

    public LMDiagMsg parseJsonLmDiagMsg(String json) throws Exception {
        //验证参数完整
        ObjectMapper objectMapper = new ObjectMapper();
        LMDiagMsg lmDiagMsg = objectMapper.readValue(json, LMDiagMsg.class);
        //格式化参数
        String filterJoon = filterImp.doJsonFilter(objectMapper, json);

        return lmDiagMsg;
    }

    //取得大模型返回信息
    public LmAppMessage parseJsonLmLmResult(String json) throws Exception {
        //验证参数完整
        ObjectMapper objectMapper = new ObjectMapper();
        // 获取根节点
        JsonNode rootNode = objectMapper.readTree(json);
        LmAppMessage lmAppMessage = new LmAppMessage();
        SafetyRisk safetyRisk = null;
        // 获取 choices 数组
        ArrayNode choices = (ArrayNode) rootNode.get("choices");
        if (choices !=null){
            for (JsonNode choice : choices) {
                // 获取 message 字段
                JsonNode message = choice.get("message");

                // 获取 content 字段
                JsonNode content = message.get("content");
                String jsonres = "";
                // 检查 content 是否是对象且包含 value 键
                //if (content.isObject() && content.has("content"))
                if (content.isTextual()) {
                    jsonres = content.asText().replaceAll("`", "").replaceAll("\n", "").replaceAll("json", "");
                    ObjectMapper mapper = new ObjectMapper();
                    try {
                        safetyRisk = mapper.readValue(jsonres, SafetyRisk.class);
                        log.info("safetyRisk:{}", safetyRisk);
                        if(safetyRisk !=null && "yes".equals(safetyRisk.getSafetyRisk())){
                            lmAppMessage.setStatus("ALARM"); //报警有效
                        } else
                            lmAppMessage.setStatus("IGNORE");
                        lmAppMessage.setMessage("调用成功");
                    } catch (IOException e) {
                        lmAppMessage.setStatus("ERROR");//
                        lmAppMessage.setMessage(e.getMessage());
                        e.printStackTrace();
                    }
                }
            }
            return lmAppMessage;
        } else {
            lmAppMessage.setStatus("ERROR");//
            lmAppMessage.setMessage("大模型调用失败");
            return lmAppMessage;
        }
    }

    public ChatCompletion parseJsonChatCompletion(String json) throws Exception {
        //验证参数完整
        ObjectMapper objectMapper = new ObjectMapper();
        ChatCompletion object = objectMapper.readValue(json, ChatCompletion.class);
        //格式化参数
        //  String filterJoon = filterImp.doJsonFilter(objectMapper, json);

        return object;
    }

    public VideoAlarmInformation parseJsonVideoAlarm(String json) throws Exception {
        //验证参数完整
        ObjectMapper objectMapper = new ObjectMapper();
        VideoAlarmInformation lmDiagMsg = objectMapper.readValue(json, VideoAlarmInformation.class);
        //格式化参数
//        String filterJoon = objectMapper
//        // filterImp.doJsonFilter(objectMapper, json);

        return lmDiagMsg;
    }

    public String toJson(LMDiagMsg request) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(request);
    }

    public String toJsonLmAppMessage(LmAppMessage request) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(request);
    }
}